﻿<Serializable()>
Public Class CarEditor
    Private LinkedCarValue As L3DObjectsLibrary.Car
    Private DB As New DateBuffer
    Private DateTransferMode As Boolean
    Dim OldKmLocal As Km
    Private Structure DateBuffer
        Dim DStart As Date
        Dim DEnd As Date
        Dim RDStart As Date
        Dim RDEnd As Date
    End Structure
    Private Structure Km
        Dim City As Integer
        Dim RG As Integer
        Dim HS As Integer
        Dim Status As Integer
    End Structure
    Private Property OldKm As Km
        Get
            Return OldKmLocal
        End Get
        Set(ByVal value As Km)
            OldKmLocal = value
        End Set
    End Property
    Public Property LinkedCar As L3DObjectsLibrary.Car
        Get
            Return LinkedCarValue
        End Get
        Set(ByVal value As L3DObjectsLibrary.Car)
            LinkedCarValue = value
            FillFromLinkedCar()
        End Set
    End Property
    Private Overloads Property Name As String
        Get
            Return lblName.Text
        End Get
        Set(ByVal value As String)
            lblName.Text = value
        End Set
    End Property
    Public Event CloseRequest(ByVal sender As L3DObjectsLibrary.CarEditor, ByVal CloseStatus As Microsoft.VisualBasic.MsgBoxResult)
    Public Sub Reset()
        With OldKmLocal
            txtKmCity.Value = .City
            txtKmRG.Value = .RG
            txtKmHS.Value = .HS
            txtKmStatus.Value = .Status
        End With
    End Sub
    Private Sub FillFromLinkedCar()
        Dim CmdAutoStatus As Boolean
        If LinkedCarValue.CarStatus = L3DObjectsLibrary.Car.L3DCarStatusStop Then
            CmdAutoStatus = False
        Else
            CmdAutoStatus = True
        End If
        cmdAuto1.Enabled = CmdAutoStatus
        cmdAuto2.Enabled = CmdAutoStatus
        With LinkedCarValue
            lblName.Text = .Name
            txtName.Text = .Name
            txtKmCity.Value = .kmCity
            txtKmHS.Value = .kmHS
            txtKmRG.Value = .kmRG
            txtKmStatus.Value = .kmStatus
            dateCarEndDate.Value = .CarEndDate
            dateCarStartDate.Value = .CarStartDate
            dateCarRealEndDate.Value = .CarRealEndDate
            dateCarRealStartDate.Value = .CarRealStartDate
            chkEOBD.Checked = .EOBD
            cmbStatus.SelectedIndex = .CarStatus
        End With
        With OldKmLocal
            .City = txtKmCity.Value
            .RG = txtKmRG.Value
            .HS = txtKmHS.Value
            .Status = txtKmStatus.Value
        End With
        FillDateBuffer()
        txtKmStatus.Focus()
        txtKmStatus.Select(0, Len(Str(txtKmStatus.Value)))
    End Sub
    Private Sub FillDateBuffer()
        With DB
            .DStart = dateCarStartDate.Value
            .DEnd = dateCarEndDate.Value
            .RDStart = dateCarRealStartDate.Value
            .RDEnd = dateCarRealEndDate.Value
        End With
    End Sub
    Private Sub ControlsHandlersAdd(ByVal Father As Object)
        For Each Control As Windows.Forms.Control In Father.Controls
            If Control.HasChildren = True Then
                ControlsHandlersAdd(Control)
            End If
            If Control.GetType = GetType(Windows.Forms.TextBox) _
                    Or Control.GetType = GetType(Windows.Forms.DateTimePicker) _
                    Or Control.GetType = GetType(Windows.Forms.ComboBox) _
                    Or Control.GetType = GetType(Windows.Forms.NumericUpDown) _
                    Or Control.GetType = GetType(Windows.Forms.CheckBox) Then
                AddHandler Control.KeyUp, AddressOf Key_UP
            End If
        Next
    End Sub
    Private Sub Key_UP(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtKmCity.KeyUp
        If e.KeyCode = Windows.Forms.Keys.Enter Then
            cmdOK.PerformClick()
        ElseIf e.KeyCode = Windows.Forms.Keys.Escape Then
            cmdCancel.PerformClick()
        Else
            lblName.Text = txtName.Text
        End If
    End Sub
    Private Sub cmdClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdCancel.Click, cmdOK.Click
        Dim Feedback As Microsoft.VisualBasic.MsgBoxResult
        If sender Is cmdCancel Then
            RaiseEvent CloseRequest(Me, vbCancel)
            Reset()
            Feedback = vbCancel
        ElseIf sender Is cmdOK Then
            RaiseEvent CloseRequest(Me, vbOK)
            With LinkedCarValue
                .Name = txtName.Text
                .CarStartDate = dateCarStartDate.Value
                .CarEndDate = dateCarEndDate.Value
                .CarRealStartDate = dateCarRealStartDate.Value
                .CarRealEndDate = dateCarRealEndDate.Value
                .EOBD = chkEOBD.Checked
                .CarStatus = cmbStatus.SelectedValue
            End With
            Feedback = vbOK
        End If
    End Sub
    Private Sub cmdAuto_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdAuto1.Click, cmdAuto2.Click
        Dim Button As Windows.Forms.Button = sender
        DateTransferMode = True
        If Button Is cmdAuto1 Then
            dateCarEndDate.Value = LinkedCarValue.EndDateForecast(dateCarStartDate.Value)
        Else
            dateCarRealEndDate.Value = LinkedCarValue.EndDateForecast(dateCarRealStartDate.Value)
        End If
        DateTransferMode = False
    End Sub
    Private Sub txtKm_ValueChanged(ByVal sender As System.Object, ByVal e As Windows.Forms.KeyEventArgs) Handles txtKmStatus.KeyUp, txtKmCity.KeyUp, txtKmHS.KeyUp, txtKmRG.KeyUp
        If sender.GetType = GetType(Windows.Forms.NumericUpDown) Then
            Dim NumericUpDown As Windows.Forms.NumericUpDown = sender
            If NumericUpDown.Text = "" Then
                NumericUpDown.Text = "0"
                NumericUpDown.Value = 0
                NumericUpDown.Select(0, 1)
            End If
        End If
    End Sub

    Private Sub txtKm_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtKmStatus.ValueChanged, txtKmCity.ValueChanged, txtKmHS.ValueChanged, txtKmRG.ValueChanged, txtKmStatus.KeyUp, txtKmRG.KeyUp, txtKmHS.KeyUp
        If sender.GetType = GetType(Windows.Forms.NumericUpDown) Then
            Dim NumericUpDown As Windows.Forms.NumericUpDown = sender
            If NumericUpDown Is txtKmStatus Then
                LinkedCarValue.kmStatus = NumericUpDown.Value
                Dim CmdAutoStatus As Boolean
                If txtKmCity.Value + txtKmHS.Value + txtKmRG.Value > txtKmStatus.Value Then
                    CmdAutoStatus = True
                Else
                    CmdAutoStatus = False
                End If
                cmdAuto1.Enabled = CmdAutoStatus
                cmdAuto2.Enabled = CmdAutoStatus
            ElseIf NumericUpDown Is txtKmCity Then
                LinkedCarValue.kmCity = NumericUpDown.Value
            ElseIf NumericUpDown Is txtKmRG Then
                LinkedCarValue.kmRG = NumericUpDown.Value
            ElseIf NumericUpDown Is txtKmHS Then
                LinkedCarValue.kmHS = NumericUpDown.Value
            End If
        End If
    End Sub
    Private Sub CarEditor_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        ControlsHandlersAdd(Me)
    End Sub

    Private Sub CarEditor_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
        Dim g As System.Drawing.Graphics = e.Graphics
        Dim Rect As New System.Drawing.Rectangle(1, 1, Me.Width - 3, Me.Height - 3)
        Dim MyPen As New System.Drawing.Pen(New System.Drawing.SolidBrush(Drawing.ColorTranslator.FromHtml("#000000")), 1)
        g.SmoothingMode = Drawing.Drawing2D.SmoothingMode.HighQuality
        VMGenLibrary.Graphics.DrawRoundedRectangle(g, MyPen, Rect, New System.Drawing.Size(20, 20), Drawing.ColorTranslator.FromHtml("#FFFFC0"))

    End Sub
    Private Sub cmdUpDown_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdUp.Click, cmdDown.Click
        Dim Command As Windows.Forms.Button = sender
        DateTransferMode = True
        If sender Is cmdUp Then
            dateCarEndDate.Value = dateCarRealEndDate.Value
            dateCarStartDate.Value = dateCarRealStartDate.Value
        ElseIf sender Is cmdDown Then
            dateCarRealEndDate.Value = dateCarEndDate.Value
            dateCarRealStartDate.Value = dateCarStartDate.Value
        End If
        DateTransferMode = False
    End Sub

    Private Sub dateCar_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles dateCarEndDate.ValueChanged, dateCarStartDate.ValueChanged, dateCarRealEndDate.ValueChanged, dateCarRealStartDate.ValueChanged
        Dim DiffBuffer As Integer
        If DateTransferMode = False And Me.Visible = True Then
            If sender Is dateCarStartDate Then
                DiffBuffer = DateDiff(DateInterval.Day, DB.DStart, dateCarStartDate.Value)
                dateCarEndDate.Value = DateAdd(DateInterval.Day, DiffBuffer, DB.DEnd)
            ElseIf sender Is dateCarRealStartDate Then
                DiffBuffer = DateDiff(DateInterval.Day, DB.RDStart, dateCarRealStartDate.Value)
                dateCarRealEndDate.Value = DateAdd(DateInterval.Day, DiffBuffer, DB.RDEnd)
            ElseIf sender Is dateCarEndDate Then
                If DateTime.Compare(dateCarStartDate.Value, dateCarEndDate.Value) > 0 Then
                    dateCarEndDate.Value = dateCarStartDate.Value
                End If
            ElseIf sender Is dateCarRealEndDate Then
                If DateTime.Compare(dateCarRealStartDate.Value, dateCarRealEndDate.Value) > 0 Then
                    dateCarRealEndDate.Value = dateCarRealStartDate.Value
                End If
            End If
        End If
        FillDateBuffer()
    End Sub
End Class
